﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace PerformanceCounterTestApp
{
    public class PerformanceInfo
    {
        PerformanceCounterCategory category;
        string processName;
        string prefix = "....";
        string sep = " - ";

        private List<KeyValuePair<string, long>> values;
        private List<string> counterNames = new List<string>();

        public PerformanceInfo(string categoryName, string processName)
        {
            category = new PerformanceCounterCategory(categoryName);
            this.processName = processName;
        }
        
        public PerformanceInfo(string categoryName, string processName, IEnumerable<string> counterNames)
            : this(categoryName, processName)
        {
            this.counterNames.AddRange(counterNames);            
        }

        public PerformanceInfo Aquire()
        {
            values = new List<KeyValuePair<string, long>>();            
            try
            {
                foreach (PerformanceCounter pc in GetCounters())
                {
                    values.Add(new KeyValuePair<string, long>(pc.CounterName, pc.RawValue)); 
                }
            }
            catch
            {
            }
            return this;
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, long> kv in values)
            {                
                sb.Append(prefix).Append(kv.Key).Append(sep).AppendLine(kv.Value.ToString());
            }            
            return sb.ToString();
        }            

        IEnumerable<PerformanceCounter> GetAllCountersForCategory()
        {
            if (category.CategoryType == PerformanceCounterCategoryType.SingleInstance)
                return category.GetCounters();       
            return category.GetCounters(processName);            
        }

        IEnumerable<PerformanceCounter> GetCounters()
        {
            if (counterNames.Count == 0)
                return GetAllCountersForCategory();

            List<PerformanceCounter> pcs = new List<PerformanceCounter>();
            foreach (string counterName in counterNames)
            {

                if (category.CounterExists(counterName))
                {
                    if (category.CategoryType == PerformanceCounterCategoryType.SingleInstance)
                    {
                        pcs.Add(new PerformanceCounter(category.CategoryName, counterName));
                    }
                    else
                    {
                        pcs.Add(new PerformanceCounter(category.CategoryName, counterName, processName));
                    }
                }
            }
            return pcs;
        }
    }
}
/*
            long l = pc.RawValue;
            if (l <= 0xFFFF)
                return l.ToString();
            long k = (l >> 10);
            long b = l & 0x3ff;
            if (l <= 0x0100000)
                return string.Format("{0}k {1}", k ,b);
            long m = l >> 20;
            k = k & 0x3ff;            
            return string.Format("{0}M {1}k {2}", m, k, b);
             * */